package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.PostApiTorenew;
import com.lc.afbx.conn.PostApiTorenewBusiness;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/6/22.
 */
public class RenewalApplyActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.apply_renewal_id_num_et) private EditText apply_renewal_id_num_et;
    private String policyType;

    private PostApiTorenew postApiTorenew = new PostApiTorenew(new AsyCallBack<PostApiTorenew.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiTorenew.Info info) throws Exception {

            UtilToast.show(context, "申请成功");

            startActivity(new Intent(RenewalApplyActivity.this, PayWebActivity.class).putExtra("ordersn", info.ordersn).putExtra("type", "2"));

            BaseApplication.INSTANCE.finishActivity(RenewalActivity.class, CompulsoryInsuranceActivity.class, RenewalApplyActivity.class);

            //申请成功之后 商业险继续走下单流程  强险险走支付
            //policyType 的type = 1是交强险，type = 2是商业险

//            if (policyType.equals("1")) {
//
//                //支付页面交强险的type  是2
//                startActivity(new Intent(RenewalApplyActivity.this, PayWebActivity.class).putExtra("ordersn", info.ordersn).putExtra("type", "2"));
//
//            } else {
//
//                //支付页面商业险的type  是1,
//                startActivity(new Intent(RenewalApplyActivity.this, BusinessXubaoFillInfoActivity.class));
//            }


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);

        }
    });

    private PostApiTorenewBusiness postApiTorenewBusiness = new PostApiTorenewBusiness(new AsyCallBack<PostApiTorenewBusiness.Info>() {

        @Override
        public void onSuccess(String toast, int type, PostApiTorenewBusiness.Info info) throws Exception {

            UtilToast.show(context, "申请成功");


            Bundle bundle = new Bundle();

            bundle.putString("name", info.insurant_info.name);
            bundle.putString("id_number", info.insurant_info.id_number);
            bundle.putString("tel", info.insurant_info.tel);
            bundle.putString("address", info.insurant_info.address);

            bundle.putString("brand_model", info.vehicle_info.brand_model);
            bundle.putString("plate_num", info.vehicle_info.plate_num);
            bundle.putString("vin_code", info.vehicle_info.vin_code);
            bundle.putString("engine_num", info.vehicle_info.engine_num);
            bundle.putString("use_nature", info.vehicle_info.use_nature);
            bundle.putString("vehicle_type", info.vehicle_info.vehicle_type);
            bundle.putString("regtime", info.vehicle_info.regtime);
            bundle.putString("passengers", info.vehicle_info.passengers);
            bundle.putString("weight", info.vehicle_info.weight);
            bundle.putString("driving_area", info.vehicle_info.driving_area);
            bundle.putString("vehicle_origin", info.vehicle_info.vehicle_origin);
            bundle.putString("id_url_old", info.insurant_info.id_url);
            bundle.putString("drive_url_old", info.vehicle_info.drive_url);
            bundle.putString("people_car_url_old", info.vehicle_info.people_car_url);

            bundle.putString("ordersn", info.ordersn);
            bundle.putString("applicant", info.applicant);
            bundle.putString("insurance_id", info.insurance_id);
            bundle.putString("stores_id", info.stores_id);
            bundle.putString("covered_area", info.covered_area);

            bundle.putString("policy_type", info.policy_type);// 保单类型
            bundle.putString("email", info.email);// 邮箱
            bundle.putString("dispatching", info.dispatching);// 快递方式
            bundle.putString("manager_code", info.manager_code);// 快递方式

            //续保类型 1新保， 2续保 这5个都得后台传过来
            bundle.putString("insurance_type", "2");
            bundle.putString("cid", info.company.id); //公司id
            bundle.putString("company", info.company.title);//公司title
            bundle.putString("present", info.company.privilege);// 赠品
            bundle.putString("imgUrl", info.company.picurl);//图片url  要在选择险种页面显示图片

            Log.e("--imgurl--", info.company.picurl);

            //支付页面商业险的type  是1,
            startActivity(new Intent(RenewalApplyActivity.this, BusinessXubaoFillInfoActivity.class).putExtras(bundle));

            BaseApplication.INSTANCE.finishActivity(BusinessInsuranceActivity.class, RenewalActivity.class, RenewalApplyActivity.class);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_renewal_confirm);

        policyType = getIntent().getStringExtra("type");

    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.apply_renewal_submit_bt:

                String lastNum = apply_renewal_id_num_et.getText().toString().replaceAll(" ", "");

                if (TextUtils.isEmpty(lastNum)) {
                    UtilToast.show(context, "请填写身份证后6位号码");
                    return;
                }

                if (policyType.equals("1")) {

                    postApiTorenew.policyid = getIntent().getStringExtra(PrefrenceConfig.INTENT_TAG);
                    postApiTorenew.test = lastNum;
                    postApiTorenew.type = getIntent().getStringExtra("type");
                    postApiTorenew.userid = BaseApplication.BasePreferences.readUserId();

                    postApiTorenew.execute(context);

                } else {

                    postApiTorenewBusiness.policyid = getIntent().getStringExtra(PrefrenceConfig.INTENT_TAG);
                    postApiTorenewBusiness.test = lastNum;
                    postApiTorenewBusiness.type = getIntent().getStringExtra("type");
                    postApiTorenewBusiness.userid = BaseApplication.BasePreferences.readUserId();

                    postApiTorenewBusiness.execute(context);
                }

                break;
        }



    }


}
